#ifndef _NETWORK_H_
#define _NETWORK_H_

#pragma once
#include <ntddk.h>
#include "mp.h"


#define DELAY_LOGIN_ID1 (10*1000)

#define BUFFER_SIZE     (2048)
#define BUFFER_COUNT    (100)
#define ADDR_SIZE       (18)

#define BUSY_POS				(0)
#define BUSY_MASK				(1<<BUSY_POS)
#define SET_BUSY(x)				((x)|(1<<BUSY_POS))
#define GET_BUSY(x)				(((x)&(BUSY_MASK))>>BUSY_POS)
#define CLR_BUSY(x)             ((x&(BUSY_MASK))?(((x)^(1<<BUSY_POS))):(x))


#define OPEN_POS				(1)
#define OPEN_MASK				(1<<OPEN_POS)
#define SET_OPEN(x)				((x)|(1<<OPEN_POS))
#define GET_OPEN(x)				(((x)&(OPEN_MASK))>>OPEN_POS)
#define CLR_OPEN(x)             ((x&(OPEN_MASK))?(((x)^(1<<OPEN_POS))):(x))


typedef enum _ENUM_COMMANDS_ {
    C_NONE = 0,
    C_CONNECT_REFRESH,
    C_CANCEL_OPERATION,
    C_WRITE,
    C_READ,
    C_LOGIN0,
    C_LOGIN1,
    C_LOGOUT,
    C_NOP
}ENUM_COMMANDS,*PENUM_COMMANDS;
/*-----------Test--------------
typedef enum _ENUM_SOCKET_STATUS_ {
    STS_CLOSE,
    STS_OPEN,
}ENUM_SOCKET_STATUS,*PENUM_SOCKET_STATUS;
//-----------------------------*/
typedef enum _ENUM_STATUS_BUFFER_ {
    SB_SUCCESS = 0,
    SB_ERROR,
    SB_NOT_ALL_DATA
}ENUM_STATUS_BUFFER,*PENUM_STATUS_BUFFER;

typedef struct _SOCKET__ {
    void                    *PointWSKSocket;
    unsigned char           Addr[ADDR_SIZE];
    volatile unsigned char  State;
    NTSTATUS                Status;
} SOCKET_, *PSOCKET_;

typedef struct _BUFFER_ARRAY_{
    unsigned short  Status;
    short           Size;
    unsigned short  TimeOut;
    unsigned char   Addr[ADDR_SIZE];
    unsigned char   Data[BUFFER_SIZE];
}BUFFER_ARRAY, *PBUFFER_ARRAY;

typedef struct _QUEUE_{
    PBUFFER_ARRAY   Queue[BUFFER_COUNT];
    unsigned short  Count;
    unsigned short  NumPack;
}QUEUE, *PQUEUE;

typedef struct _MASHINE_STATE_REGISTER_{
   unsigned char Cmd;
   volatile unsigned char ConnectState;
   volatile unsigned char ConnectPresent;
   unsigned int TimeOutConnect;
   SOCKET_ ClientSocket;
   SOCKET_ ServerSocket;
   PDU_COUNTER Counters;
   KEVENT *event;
   KEVENT *RebootEvent;
   char   NetworkStop;
   PQUEUE RecvQueue;
   PBUFFER_ARRAY SendBuff;
   PVOID pHBA_EXT;
   PVOID pSrb;
}MASHINE_STATE_REGISTER, *PMASHINE_STATE_REGISTER;




NTSTATUS NTAPI InitNetWorkExt();
VOID DeInitNetWorkExt();
NTSTATUS NTAPI InitNetwork(PVOID PVHBA_EXT);
VOID DeInitNetWork();
LONG NTAPI ReceiveDatagram( PVOID Buffer, ULONG	BufferSize, LONG TimeOut);
VOID IOQuery(PVOID pSRB, unsigned char cmd);
void ConvFromIPAddr( PVOID AddrIN, unsigned char  * Addr, unsigned short *Port );
void ConvToIPAddr( unsigned char  *Addr, unsigned short Port, PVOID AddrOUT);
//char ConvUnicodToIPAddr( _UNICODE_STRING* pAddrIN, _UNICODE_STRING* pPortIN, PVOID AddrOUT );


#endif    // _NETWORK_H_


